package com.coffee.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coffee.common.annotation.Log;
import com.coffee.common.result.R;
import com.coffee.common.util.ExcelUtil;
import com.coffee.system.common.dto.SysUserAddDTO;
import com.coffee.system.common.dto.SysUserEditDTO;
import com.coffee.system.common.dto.SysUserQueryDTO;
import com.coffee.system.common.dto.SysUserResetPwdDTO;
import com.coffee.system.entity.SysUser;
import com.coffee.system.service.ISysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysUser")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @SaCheckPermission("system:sysUser:page")
    @Log(title = "用户管理分页查询")
    public R page(Page reqPage, SysUserQueryDTO req) {
        return R.success(sysUserService.page(reqPage, req));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @SaCheckPermission("system:sysUser:add")
    @Log(title = "用户管理新增")
    public R add(@Validated @RequestBody SysUserAddDTO req) {
        sysUserService.add(req);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    @SaCheckPermission("system:sysUser:edit")
    @Log(title = "用户管理修改")
    public R edit(@Validated @RequestBody SysUserEditDTO req) {
        sysUserService.edit(req);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @SaCheckPermission("system:sysUser:remove")
    @Log(title = "用户管理删除")
    public R remove(@RequestParam String ids) {
        sysUserService.remove(ids);
        return R.success();
    }

    /**
     * 查看
     */
    @GetMapping("/view")
    @SaCheckPermission("system:sysUser:view")
    @Log(title = "用户管理查看")
    public R view(@RequestParam String id) {
        return R.success(sysUserService.view(id));
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @SaCheckPermission("system:sysUser:export")
    @Log(title = "用户管理导出")
    public R export(SysUserQueryDTO req) {
        String filepath = ExcelUtil.export("用户列表", SysUser.class, sysUserService.list(req));
        return R.success(filepath);
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPwd")
    @SaCheckPermission("system:sysUser:resetPwd")
    @Log(title = "用户管理导出")
    public R resetPwd(@Validated @RequestBody SysUserResetPwdDTO req) {
        sysUserService.resetPwd(req);
        return R.success();
    }

}

